<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/11
 * Time: 16:01
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Notice extends Base {
    public function index(){
        if(request()->isAjax()){
            $map['notice_cate'] = -1; // 后台发布的消息
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['ua.username|a.notice_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['a.notice_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['a.notice_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['a.notice_time'] = ['between time',[$start,$end]];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('notice')->alias('a')
                ->join('up_admin ua','ua.id = a.notice_send_user','left')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('notice')->alias('a')
                ->join('up_admin ua','ua.id = a.notice_send_user','left')
                ->where($map)
                ->field('a.*,ua.username')
                ->page($NowPage,$limits)
                ->order('notice_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }


    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function add(){
        if(Request::instance()->isPost()){
            $notice_title = trim(input('notice_title'), ' ');
            $notice_thumb = input('notice_thumb');
            $notice_type = input('notice_type');
            $notice_content = input('notice_content');
            $notice_user_id = input('notice_user_id');
            $data = [
                'notice_title' => $notice_title,
                'notice_thumb' => $notice_thumb,
                'notice_time' => date('Y-m-d H:i:s'),
                'notice_type' => $notice_type,
                'notice_content' => $notice_content,
                'notice_send_user'=>session('uid'),
                'notice_read_user' => ',0,',
                'notice_del_user' => ',0,'
            ];
            if($notice_type == 1){
                $data['notice_user'] = -1;
            }else{
                $data['notice_user'] = ',' . $notice_user_id . ',';
            }

            $res = Db::table('up_notice')->insertGetId($data);
            if($res){
                try{
                    if($notice_type == 1){
                        $this->Jpush(2, '', $notice_title, ['send_type' => 1, 'notice_type' => $notice_type, 'notice_id' => $res]);
                    }else{
                        $this->Jpush(1, (string)$notice_user_id, $notice_title, ['send_type' => 1, 'notice_type' => $notice_type, 'notice_id' => $res]);
                    }
                }catch(\Exception $e){
                    
                }
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $user = Db::table('up_u_user')
            ->where('user_status', 1)
            ->field('user_id, user_username')
            ->select();
        $this->assign('user', $user);
        return $this->fetch();
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_notice')->where('notice_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }

    /**
     * 详情
     * @return \think\response\Json
     */
    public function detail(){
        $notice_id = input('notice_id');
        $res = Db::table('up_notice')->where('notice_id', $notice_id)->find();
        if($res['notice_type']==1){
           $res['notice_type_name'] = '系统消息';
        }else{
            $res['notice_type_name'] = '个人消息';
        }
        $this->assign('res',$res);
        return $this->fetch();
    }
}